﻿@using DragonSolution.Web.Common.Common;
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>新闻列表</title>
</head>
<body>
    <div class="Mtop"></div>
    <div class="Mcont">
        <div class="Mleft">@Html.Partial("PartialAdminLeft")</div>
        <div class="Mright">
            <div class="top">
                <div class="btn">
                    <a href="/Account/logOff"><div class="link"></div></a>
                    <a target="_blank" href="@ConstDim.IndexPath"><div class="link"></div></a>
                </div>
                <div class="site">您的位置 / 新闻中心 / 新闻列表</div>
            </div>
            <div class="mid" style="padding:5px">
                <div class="mid_left_category">
                    <h4>新闻类型</h4>
                    <div class="treeBox">
                        <ul id="treeUl" class="ztree" style="border: 0"></ul>
                    </div>
                </div>
                <div class="mid_right_list">
                    <div class="table_box">
                        <h4>查询条件</h4>
                        <table class="table_box">
                            <tbody>
                                <tr>
                                    <th width="80" align="right">新闻名称：</th>
                                    <th width="130" align="left"><input type="text" id="query_name" /></th>
                                    <th width="80" align="right">发布时间：</th>
                                    <th width="130" align="left">
                                        <input type="text" id="query_start_time" class="laydate-icon" readonly="readonly" style="width:90px" />至
                                        <input type="text" id="query_end_time" class="laydate-icon" readonly="readonly" style="width:90px" />
                                    </th>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="4">
                                        <input type="button" class="table_btn" value="查询" onclick="Query()" />
                                        <input type="button" class="table_btn" value="重置" onclick="Reset()" />
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <div class="table_box">
                        <div class="table_box">
                            <h4>
                                新闻列表
                            </h4>
                            <div class="table_box_toolbar">
                                <a onclick="Add()">添加</a>
                            </div>
                            <table class="table_box">
                                <thead>
                                    <tr>
                                        <th width="40">序号</th>
                                        <th>名称</th>
                                        <th width="90">发布时间</th>
                                        <th width="70">推介首页</th>
                                        <th width="100">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="tbody"></tbody>
                                <tfoot id="tfoot">
                                    <tr><td colspan="5" id="splitPage"></td></tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--树控件-->
    <link href="~/CommonResource/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <script src="~/CommonResource/zTree/js/jquery.ztree.core-3.5.min.js"></script>
    <!--日期-->
    <script src="~/CommonResource/laydate/laydate-min.js"></script>
    <link href="~/CommonResource/laydate/skins/danlan/laydate.css" rel="stylesheet" />
    <!--分页-->
    <script src="~/CommonResource/Js/SpiltPage.js"></script>
    <link href="~/CommonResource/Css/SpiltPage.css" rel="stylesheet" />
    <script src="~/CommonResource/Js/Common.js"></script>
    <script type="text/javascript">
        var start = {
            elem: '#query_start_time',
            format: 'YYYY-MM-DD',
            istime: false,
            istoday: true,
            choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#query_end_time',
            format: 'YYYY-MM-DD',
            istime: false,
            istoday: true,
            choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        var pageCount;
        var currentIndex=0;
        var typeId=0;
        var pageSize=@ConstDim.AdminPageSize;
        var treeObj = null;
        var treeJson = null;
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClick
            }
        };
        //树节点点击事件
        function onClick(event, treeId, treeNode, clickFlag) {
            clickNode(treeNode);
        }

        //列表html模板
        var tBodyTPL = "<tr>\
                            <td>{Number}</td>\
                            <td align=\"left\" style=\"padding-left:10px\">{Name}</td>\
                            <td>{Time}</td>\
                            <td>{IsTop}</td>\
                            <td width=\"100\">\
                                <a onclick=\"Edit({ID})\">编辑</a>&nbsp;\
                                <a onclick=\"Delete({ID})\">删除</a>\
                            </td>\
                        </tr>";

        $(function () {
            //展开当前二级菜单
            var $this = $("#Mul li[liName='news'] > a");
            $this.next("ul").slideToggle();
            changeIcon($this);
            //时间
            laydate(start);
            laydate(end);

            //树形菜单
            getCategoryList(typeId);
        });

        //树形菜单
        function getCategoryList(typeId){
            var params = {};
            params.action = "GetCategoryList";
            $.ajax({
                url: "/Areas/AdminMrg/Ashx/Mnews.ashx",
                data: params,
                type: "POST",
                dataType: "json",
                success: function (msg) {
                    if (msg.OperateResult == "操作成功"){
                        treeJson = msg.ResponseData;
                        treeObj = $.fn.zTree.init($("#treeUl"), setting, treeJson);
                        var chooseNode;
                        if(typeId=="0")
                        {
                            //默认选中第一个节点
                            chooseNode = treeObj.getNodes()[0];
                        }
                        else{
                            //parentId不为空，选中此类别
                            var chooseNodes = treeObj.getNodesByParam("id", parentId);
                            if(chooseNodes.length>0){ chooseNode=chooseNodes[0]; }
                        }    
                        treeObj.selectNode(chooseNode);
                        clickNode(chooseNode);
                    }
                },
                error: function (e) {
                    alert("加载出错");
                }
            });
        }

        //点击节点事件
        function clickNode(treeNode){
            if(treeNode.open){
                treeObj.expandNode(treeNode,false);
            } else{
                treeObj.expandNode(treeNode,true);
            }
            typeId=treeNode.id;//父id
            currentIndex=0;//页索引重新初始化
            //加载数据
            loadData(typeId,currentIndex,pageSize);
        }

        //加载数据(异步加载)
        //typeId分类id，pageIndex页索引（0表示第一页）,pageSize页大小（默认10条）
        function loadData(typeId,currentIndex,pageSize){
            var params = {};
            params.action = "GetSpiltPageNewsList";
            params.typeId = typeId;
            params.pageIndex = currentIndex;
            params.pageSize = pageSize;
            params.queryName=$.trim($("#query_name").val());
            params.queryStartTime=$("#query_start_time").val();
            params.queryEndTime=$("#query_end_time").val();
            $.ajax({
                url: "/Areas/AdminMrg/Ashx/Mnews.ashx",
                data: params,
                type: "POST",
                dataType: "json",
                success: function (msg) {
                    if (msg.OperateResult == "操作成功"){
                        CreateHtml(msg.ResponseData,currentIndex,pageSize);
                    }
                },
                error: function (e) {
                    alert("加载出错");
                }
            });
        }

        //创建列表html
        function CreateHtml(obj,currentIndex,pageSize){
            var HTML ="";
            var list=obj.DataList;
            if(list.length>0){
                for(var i=0;i<list.length;i++){
                    var item=list[i];
                    HTML += tBodyTPL.format({
                        "Number": (i + 1),
                        "Name": item.NewsName,
                        "Time":ChangeDateFormat(item.NewsPostTime),
                        "IsTop":item.IsTop==1?"是":"否",
                        "ID":item.NewsID
                    });
                }
                var params={};
                params.containerId="splitPage";
                params.pageIndex=currentIndex;
                params.pageSize=pageSize;
                params.totalCount=obj.TotalCount;
                params.totalPages=obj.TotalPages;
                params.hasPreviousPage=obj.HasPreviousPage;
                params.hasNextPage=obj.HasNextPage;
                $.spiltpage.CreateSpiltPageHtml(params);
                $("#tfoot").show();

                pageCount=obj.TotalPages;
            }
            else{
                HTML="<tr><td colspan=\"5\">暂无数据</td></tr>";
                $("#tfoot").hide();
            }
            $("#tbody").html(HTML);
        } 
        
        //首页
        function goHome(){
            currentIndex=0;
            loadData(typeId,currentIndex,pageSize);
        }

        //上一页
        function goPro(){
            currentIndex--;
            loadData(typeId,currentIndex,pageSize);
        }

        //下一页
        function goNext(){
            currentIndex++;
            loadData(typeId,currentIndex,pageSize);
        }

        //尾页
        function goLast(){
            currentIndex=pageCount-1;
            loadData(typeId,currentIndex,pageSize);
        }

        //重置
        function Reset(){
            $("#query_name").val("");
            $("#query_start_time").val("");
            $("#query_end_time").val("");
        }

        //查询
        function Query(){
            loadData(typeId,currentIndex,pageSize)
        }

        //新增
        function Add() {
            window.location.href="/AdminMrg/MNews/AddNews/";
        }

        //编辑
        function Edit(id) {
            window.location.href="/AdminMrg/MNews/EditNews/"+id;
        }

        //删除
        function Delete(id){            
            var re=confirm("确定删除吗？");
            if(re){
                var params = {};
                params.action = "DeleteNews";
                params.id = id;
                $.ajax({
                    url: "/Areas/AdminMrg/Ashx/Mnews.ashx",
                    data: params,
                    type: "POST",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.OperateResult == "操作成功"){
                            alert("操作成功");
                            loadData(typeId,currentIndex,pageSize);
                        }
                    },
                    error: function (e) {
                        alert("操作失败");
                    }
                });
            }
        }
    </script>
</body>
</html>
